﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Imaginecup.Recycling.GameMan
{
    public class ResourceManager
    {
        private static ResourceManager Instance = null;
        private Dictionary<string, Texture2D> m_Dictionary;
      
        #region 싱글톤으로 객체 생성 제한
        public static ResourceManager GetInstance()
        {
            if (Instance == null)
            {
                Instance = new ResourceManager();
            }
            return Instance;
        }
        #endregion


        private ResourceManager()
        {
            m_Dictionary = new Dictionary<string, Texture2D>();
        }

        public void SetTexture(String key, Texture2D texture)
        {
            if (m_Dictionary.ContainsKey(key))
            {
                return;
            }
            texture.Name = key;
            m_Dictionary.Add(key, texture);
        }

        public Texture2D GetContent(string key)
        {
            if (m_Dictionary.ContainsKey(key))
            {
                return m_Dictionary[key];
            }
            return null;
        }

        public void DeleteTexture(string key)
        {
            if (!m_Dictionary.ContainsKey(key)) return;

            m_Dictionary[key].Dispose();
            m_Dictionary.Remove(key);
        }

        public void Release()
        {
            foreach (KeyValuePair<String, Texture2D> texture_Pair in m_Dictionary)
            {
                texture_Pair.Value.Dispose();
            }
            m_Dictionary.Clear();
        }
    }
}
